package com.xhd.pagination;

import java.util.Hashtable;
import java.util.Vector;

import com.xhd.base.util.xBaseObj;
import com.xhd.exception.XException;
import com.xhd.server.ServerConfig;
import com.xhd.util.DataFormat;
import com.xhd.util.DataRelation;

public class DataPaging {
	private int jm614 = 0;
	  private int jm615 = 0;
	  private int jm616 = 0;
	  private int jm617 = 0;
	  private int jm142 = 0;
	  private int jm618 = 0;
	  private String jm619 = null;

	  public DataPaging(String paramString)
	  {
	    if ((paramString == null) || (paramString.length() < 1))
	      return;
	    String[] arrayOfString = DataFormat.splitString(paramString, ",");
	    for (int j = 0; j < arrayOfString.length; ++j)
	    {
	      arrayOfString[j] = arrayOfString[j].trim();
	      int i = arrayOfString[j].indexOf(61);
	      if (i < 0)
	        continue;
	      String str = arrayOfString[j].substring(0, i);
	      int k = Integer.parseInt(arrayOfString[j].substring(i + 1));
	      if ("pn".equals(str))
	      {
	        this.jm614 = k;
	      }
	      else if ("ps".equals(str))
	      {
	        this.jm615 = k;
	      }
	      else if ("pc".equals(str))
	      {
	        this.jm616 = k;
	      }
	      else if ("cn".equals(str))
	      {
	        this.jm617 = k;
	      }
	      else if ("ci".equals(str))
	      {
	        this.jm142 = k;
	      }
	      else
	      {
	        if (!("co".equals(str)))
	          continue;
	        this.jm618 = k;
	      }
	    }
	    if ((this.jm615 > 0) && (-1 == this.jm618))
	    {
	      int j = ServerConfig.getInt("page-cache-count", 60);
	      if (j > 0)
	      {
	        this.jm618 = (j / this.jm615);
	        if (j % this.jm615 <= 0)
	          return;
	        this.jm618 += 1;
	      }
	      else
	      {
	        this.jm618 = 0;
	      }
	    }
	    else
	    {
	      this.jm618 = 0;
	    }
	  }

	  public String toString()
	  {
	    StringBuffer localStringBuffer = new StringBuffer();
	    localStringBuffer.append("pn=" + this.jm614);
	    localStringBuffer.append(",ps=" + this.jm615);
	    localStringBuffer.append(",pc=" + this.jm616);
	    localStringBuffer.append(",cn=" + this.jm617);
	    localStringBuffer.append(",ci=" + this.jm142);
	    return localStringBuffer.toString();
	  }

	  public void setCacheOption(int paramInt)
	  {
	    if (paramInt < 0)
	      return;
	    this.jm618 = paramInt;
	  }

	  public void setCacheId(int paramInt)
	  {
	    this.jm142 = paramInt;
	  }

	  public int getCacheId()
	  {
	    return this.jm142;
	  }

	  private Vector jm613(Vector paramVector, int paramInt)
	  {
	    if ((paramVector == null) || (paramVector.size() < 1))
	      return null;
	    int i = this.jm615 * this.jm614;
	    if (paramInt > 0)
	      i -= paramInt;
	    int j = i + this.jm615;
	    if (j > paramVector.size())
	      j = paramVector.size();
	    Vector localVector = new Vector(j - i);
	    for (int k = i; k < j; ++k)
	      localVector.add(paramVector.get(k));
	    return localVector;
	  }

	  public void setDataPage(xBaseObj paramxBaseObj, DataRelation paramDataRelation)
	    throws XException
	  {
	    int i = getQueryStart();
	    if (this.jm618 == 0)
	      this.jm142 = 0;
	    else
	      setCacheId(PageCache.getInstance().cacheData(paramxBaseObj, paramDataRelation.getRelationName() + "s", this.jm614 * this.jm615, this.jm618 * this.jm615, i));
	    Vector localVector = paramDataRelation.getRelationData();
	    paramDataRelation.setRelationData(jm613(localVector, i));
	    paramxBaseObj.setPagination(this);
	  }

	  public void paging(xBaseObj paramxBaseObj, DataRelation paramDataRelation)
	    throws XException
	  {
	    paging(paramxBaseObj, paramDataRelation, 0, 0);
	  }

	  public void paging(xBaseObj paramxBaseObj, DataRelation paramDataRelation, int paramInt1, int paramInt2)
	    throws XException
	  {
	    Vector localVector1 = paramDataRelation.getRelationData();
	    if ((localVector1 == null) || (localVector1.size() < 1))
	    {
	      this.jm142 = 0;
	      this.jm616 = 0;
	      this.jm617 = 0;
	      if (paramxBaseObj != null)
	        paramxBaseObj.setPagination(this);
	      return;
	    }
	    if (paramInt2 < 1)
	      paramInt2 = localVector1.size();
	    setCount(paramInt2);
	    if ((this.jm614 < 0) || (this.jm614 >= this.jm616))
	      return;
	    if (this.jm618 == 0)
	      this.jm142 = 0;
	    else
	      setCacheId(PageCache.getInstance().cacheData(paramxBaseObj, paramDataRelation.getRelationName() + "s", this.jm614 * this.jm615, this.jm618 * this.jm615, paramInt1));
	    Vector localVector2 = paramDataRelation.getRelationData();
	    paramDataRelation.setRelationData(jm613(localVector2, paramInt1));
	    if (paramxBaseObj == null)
	      return;
	    paramxBaseObj.setPagination(this);
	  }

	  public int getPageCount()
	  {
	    return this.jm616;
	  }

	  public int getPageSize()
	  {
	    return this.jm615;
	  }

	  public int getCurrPage()
	  {
	    return this.jm614;
	  }

	  public void setCurrPage(int paramInt)
	  {
	    this.jm614 = paramInt;
	  }

	  public int getCount()
	  {
	    return this.jm617;
	  }

	  public void setCount(int paramInt)
	  {
	    this.jm617 = paramInt;
	    this.jm616 = (paramInt / this.jm615);
	    if (paramInt % this.jm615 <= 0)
	      return;
	    this.jm616 += 1;
	  }

	  public boolean hasNexPage()
	  {
	    return (this.jm614 < this.jm616 - 1);
	  }

	  public int getCurrPageIndex()
	  {
	    return (this.jm614 * this.jm615);
	  }

	  public int getQueryStart()
	  {
	    return (this.jm614 * this.jm615);
	  }

	  public int getQueryLimit()
	  {
	    if (this.jm618 > 1)
	      return (this.jm615 * this.jm618);
	    return this.jm615;
	  }

	  public String getCountSQL()
	  {
	    return this.jm619;
	  }

	  public void setCountSQL(String paramString)
	  {
	    this.jm619 = paramString;
	  }

	  public static void main(String[] paramArrayOfString)
	  {
	    try
	    {
	      DataRelation localDataRelation = new DataRelation("aa");
	      Vector localVector = new Vector();
	      for (int i = 0; i < 17; ++i)
	      {
	        Hashtable localHashtable = new Hashtable();
	        localHashtable.put("id", String.valueOf(i));
	        localVector.add(localHashtable);
	      }
	      localDataRelation.setRelationData(localVector);
	      DataPaging localDataPaging = new DataPaging("pn=1,ps=10,co=0");
	      localDataPaging.paging(null, localDataRelation);
	      System.out.println("Page size=" + localDataPaging.getPageSize());
	      System.out.println("Page count=" + localDataPaging.getPageCount());
	      System.out.println("Get page " + localDataPaging.jm614 + " size=" + localDataRelation.getRelationData().size());
	    }
	    catch (Exception localException)
	    {
	      localException.printStackTrace();
	    }
	  }
}
